package petstore.web.servlet.validateCode;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class ValidateCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //resp.setContentType("image/jpeg");
        //resp.setHeader("Pragma", "no-cache");
        //resp.setHeader("Cache-Control", "no-cache");
        //resp.setDateHeader("Expires", 0);

        //使用前注意导包、加工件
        //1.创建验证码
        ValidateCode validateCode=new ValidateCode(100,30,4,30);//长度，高度，字符数，干扰条
        //2.将验证码存到session
        System.out.println("CODE:"+validateCode.getCode());

        HttpSession session = req.getSession();
        session.setAttribute("validateCode",validateCode.getCode());
        //3.将验证码图片发送给浏览器
        validateCode.write(resp.getOutputStream());
    }
}
